import axios from 'axios';
import { Message } from 'element-ui';
import store from '@/store';

let requestCount = 0;
const showLoading = () => {
	if (requestCount === 0) {
		store.dispatch('setLoading', true);
	}
	requestCount++;
};

const hideLoading = () => {
	requestCount--;
	if (requestCount === 0) {
		store.dispatch('setLoading', false);
	}
};

const service = axios.create({
	baseURL: '',
	timeout: 90000
});

// http 请求拦截器
service.interceptors.request.use(
	config => {
		showLoading();
		return config;
	},
	error => Promise.reject(error)
);

// http 响应拦截器
service.interceptors.response.use(
	response => {
		console.log('service.interceptors======>', response);
		hideLoading();
		return response;
	},
	error => {
		hideLoading();
		return Promise.reject(error);
	}
);

export default service;
